﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using System.Windows.Forms;
using ESRI.ArcGIS.Geometry;
using  DIWAGIS.ApplicationLayer.QuanLyMangCapNuoc;
using DIWAGIS.Modules;
namespace DIWAGIS.Modules
{
    class ConfigInterface
    {


        ErrorProvider errorProvider ;
        Helper help;

        public ConfigInterface()
        {
            errorProvider = new ErrorProvider();
            help = new Helper();
        }
        ///////////////////////////////

        //public void addTools(AxToolbarControl axToolbarControl1)
        //{

        //    //Add map navigation commands.
        //    axToolbarControl1.AddItem("esriControls.ControlsMapZoomInTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    axToolbarControl1.AddItem("esriControls.ControlsMapZoomOutTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    axToolbarControl1.AddItem("esriControls.ControlsMapPanTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    axToolbarControl1.AddItem("esriControls.ControlsMapFullExtentCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    axToolbarControl1.AddItem("esriControls.ControlsMapZoomToLastExtentBackCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    axToolbarControl1.AddItem("esriControls.ControlsMapZoomToLastExtentForwardCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    //Add map inquiry commands.
        //    axToolbarControl1.AddItem("esriControls.ControlsMapIdentifyTool", -1, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    axToolbarControl1.AddItem("esriControls.ControlsMapFindCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    axToolbarControl1.AddItem("esriControls.ControlsMapMeasureTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    axToolbarControl1.AddItem("esriControls.ControlsMapZoomToolControl", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //    //axToolbarControl1.AddItem("esriControls.esriControls.ControlsEditingEditorMenu", -1, -1, false, 0,esriCamman);
        //    axToolbarControl1.AddItem("esriControls.ControlsEditingEditorMenu", -1, -1, false, 0, esriCommandStyles.esriCommandStyleMenuBar);

        
        //    //////////////////////////

       

        //}

        //////////////////////////////

       // string[] _arrayFileLayer = new string[] {"HanhChinh", "SongHo", "GiaoThong", "Thua", "TimDuong", "TimSong", "DiemApLuc",
 //"DiemCuoiOng", "DongHoKhachHang", "DongHoTong", "Gieng", "OngNganh", "OngPhanPhoi", "OngTruyenDan", "Van","TruHong"}; // mảng chứa tên file layer
        //string[] _arrayFileLayer = new string[] { "fcTramBom", "fcKhuVucCapNuoc", "fcSuCoMangLuoi", "fcNhaMayNuoc", "fcHam", "fcTruCuuHoa", "fcThuyDai", "fcMayBom", "fcVoiCongCong", "fcDiemDauNoi", "fcThapCatAp", "fcBeChua", "fcDataLogger", "fcDiemGiamSatTC", "fcGieng", "fcVanHeThong", "fcVanDieuKhien", "fcDongHoKhachHang", "fcDongHoTong", "fcOngNhanh", "fcOngChinh" }; // mảng chứa tên file layer
        string[] _arrayFileLayer = new string[] { "CoQuanChuyenMon", "RGThuaDat", "TimDuongBo", "BeChua", "wOngTruyenDan", "wOngPhanPhoi", "wDiemDauNoi", "wDongHoCon", "wDongHoTong", "wDataLogger", "wVan", "wVanKhachHang", "wTruCuuHoa", "wDongHoCon", "wDongHoTong","wSuCo" }; // mảng chứa tên file layer
        string[] _arrayFileLayer1 = new string[] { "CoQuanChuyenMon"};
        string _duongDanFolder = AppDomain.CurrentDomain.BaseDirectory + @"\Documents\layers\";

        // add file layer vào trong axMapControl : tham số đầu vào là một IMapControl3 
        public void themLayerFile(IMapControl3 axMapControl)
        {
            String file;

            foreach (string str in _arrayFileLayer)
            {
                file = _duongDanFolder + str + ".lyr";

                this.addLayer(generalVariable.database + "." + generalVariable.schema + "." +  str, generalVariable.ICurrentWorkspace, axMapControl, str);
            }
           
          
            // generalVariable.database + ".DBO." + "fcOngChinh"
            this.ZoomtoLayerName(generalVariable.database + "." + generalVariable.schema + "." + "HanhChinh", generalVariable.ICurrentWorkspace, axMapControl, "HanhChinh");

        }
        public void themLayerFile1(IMapControl3 axMapControl)
        {
            String file;

          
            foreach (string str in _arrayFileLayer1)
            {
                file = _duongDanFolder + str + ".lyr";

                this.addLayer(generalVariable.database + "." + generalVariable.schema + "." + str, generalVariable.ICurrentWorkspace, axMapControl, str);
            }
            // generalVariable.database + ".DBO." + "fcOngChinh"
            this.ZoomtoLayerName(generalVariable.database + "." + generalVariable.schema + "." + "HanhChinh", generalVariable.ICurrentWorkspace, axMapControl, "HanhChinh");

        }


        // add layer vào Map Control , tham số là đường dẫn bảng trong database , workSpace đang làm việc , map control muốn thêm layer và tên của layer
        private void addLayer(String pLayerName, IWorkspace pWorkSpace, IMapControl3 axMapControl, String strLayerName)
        {
            ILayer pLayer=new FeatureLayerClass();
            IFeatureClass pFeatureClass;
            IFeatureWorkspace pFeatureWorkspace;
            IGeoFeatureLayer pGFLayer;
            ILayerFile layerFile = new LayerFileClass();
            //layerFile = new LayerFileClass();
            //ILayerFile layerFile = new LayerFileClass();
            try
            {
            pFeatureWorkspace = (IFeatureWorkspace)generalVariable.ICurrentWorkspace;

            pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pLayerName);

            //layerFile.Open(_duongDanFolder + strLayerName + ".lyr");
            IFeatureLayer featureLayer =new FeatureLayerClass();
                featureLayer.FeatureClass = pFeatureClass;
                var layer = (ILayer) featureLayer;
                layer.Name = featureLayer.FeatureClass.AliasName;
                axMapControl.AddLayer(layer);

            pGFLayer = (IGeoFeatureLayer)layerFile.Layer;
            pGFLayer.FeatureClass = pFeatureClass;
            pLayer = pGFLayer;
            axMapControl.AddLayer(pLayer);

       
            pLayer = null;
            pFeatureClass = null;
            pFeatureWorkspace = null;
            pGFLayer = null;
            layerFile = null;
            }
            catch (Exception ex)
            {

            }

        }
        public void ZoomtoLayerName(String pLayerName, IWorkspace pWorkSpace, IMapControl3 axMapControl, String strLayerName)
        {
            ILayer pLayer = new FeatureLayerClass();
            IFeatureClass pFeatureClass;
            IFeatureWorkspace pFeatureWorkspace;
            IGeoFeatureLayer pGFLayer;
            ILayerFile layerFile = new LayerFileClass();
            //layerFile = new LayerFileClass();
            //ILayerFile layerFile = new LayerFileClass();
            try
            {
            pFeatureWorkspace = (IFeatureWorkspace)generalVariable.ICurrentWorkspace;

            pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pLayerName);

            layerFile.Open(_duongDanFolder + strLayerName + ".lyr");

            pGFLayer = (IGeoFeatureLayer)layerFile.Layer;
            pGFLayer.FeatureClass = pFeatureClass;
            pLayer = pGFLayer;
            axMapControl.ActiveView.Extent = pLayer.AreaOfInterest;
            axMapControl.ActiveView.Refresh();
            pLayer = null;
            pFeatureClass = null;
            pFeatureWorkspace = null;
            pGFLayer = null;
            layerFile = null;
            }
            catch (Exception ex)
            {

            }

        }

        //////////////////////////////

        // remove file layer vào trong axMapControl : tham số đầu vào là một IMapControl3
        public void xoaLayerFile(IMapControl3 mapCotrol)
        {
            ILayer layer = null;

            while ((layer = mapCotrol.Map.Layers.Next()) != null)
            {
                mapCotrol.Map.DeleteLayer(layer);
            }

            generalVariable.ICurrentWorkspace = null;

        }
        public void loadFormByLayerName(string action, IFeature datatable, IGeometry shape, ILayer layer)
        {

            switch (layer.Name)
            {
                case "Đồng hồ khách hàng":
                    //MessageBox.Show(datatable.get_Value(datatable.Fields.FindField("ObjectID")).ToString());
                       frmDongHoKhachHang frmDHKH;
                       frmDHKH = new frmDongHoKhachHang();
                       frmDHKH.pFeature = datatable;
                       frmDHKH.ShowDialog();
                    break;
                case "Ống chính":
                    //MessageBox.Show("1");
                    frmOngChinh frmOPP;
                    frmOPP = new frmOngChinh();
                    frmOPP.pFeature = datatable;
                    frmOPP.ShowDialog();
                    break;
                case "Ống nhánh":
                    frmOngNhanh frmON;
                    frmON = new frmOngNhanh();
                    frmON.pFeature=datatable;
                    frmON.ShowDialog();
                     break;
             
                case "Van":
                    frmVan frmV;
                    frmV = new frmVan();
                    frmV.pFeature = datatable;
                    frmV.ShowDialog();
                     break;
              
                case "Trụ cứu hỏa":
                       frmTruCuuHoa frmTruHong;
                       frmTruHong = new frmTruCuuHoa();
                       frmTruHong.pFeature = datatable;
                       frmTruHong.ShowDialog();
                       break;
                case "Đồng hồ tổng":
                      frmDongHoTong frmDHT;
                      frmDHT = new frmDongHoTong();
                      frmDHT.pFeature = datatable;
                      frmDHT.ShowDialog();
                       break;
                case "Ống nối":

                     frmOngNoi frmOngNoi;
                     frmOngNoi = new frmOngNoi();
                     frmOngNoi.pFeature = datatable;
                     frmOngNoi.ShowDialog();
                       break;
                //case "Ống chính":
                //     frmOngTruyenDan frmOTD;
                //     frmOTD = new frmOngTruyenDan();
                //     frmOTD.pFeature = datatable;
                //     frmOTD.ShowDialog();
                //       break;
                case "Thửa":
                      frmThua frmT;
                      frmT = new frmThua();
                      frmT.pFeature = datatable;
                      frmT.ShowDialog();
                       break;

                default:
                    //MessageBox.Show(datatable.get_Value(datatable.Fields.FindField("ObjectID")).ToString());
                    
                    for (int i = 0; i < generalVariable.mapControl.LayerCount - 1; i++)
                    {
                        layer = generalVariable.mapControl.get_Layer(i);
                        loadFormByLayerName("", datatable, null, layer);
                           
                    }
                    break;

            }

           

        }

        public void validateIntField(TextBox text)
        {
            string str;
            str = text.Text.Trim();

            if (str.Length > 0)
            {
                str = help.checkInt(str, "", "Vui lòng nhập vào một số nguyên, ví dụ : 12");
                if (str != "")
                {
                    text.Text = "";
                    errorProvider.SetError(text, str);
                }
                else
                {
                    errorProvider.Dispose();
                }
            }
        }

        public void validateDoubleField(TextBox text)
        {
            string str;
            str = text.Text.Trim();

            if (str.Length > 0)
            {
                str = help.checkFloat(str, "", "Vui lòng nhập vào một số thực , ví dụ : 12 hoặc 12.5");
                if (str != "")
                {
                    text.Text = "";
                    errorProvider.SetError(text, str);
                }
                else
                {
                    errorProvider.Dispose();
                }
            }
        }

        //public void ZoomToSelectedFeatures()
        //{
        //    ESRI.ArcGIS.Carto.IActiveView pActiveView;
        //    ESRI.ArcGIS.Carto.IMap pMap;
        //    ESRI.ArcGIS.Geodatabase.IEnumFeature pEnumFeature;
        //    ESRI.ArcGIS.Geodatabase.IFeature pFeature;

        //    pMap = (IMap)m_hookHelper.FocusMap;
        //    pEnumFeature = (IEnumFeature)pMap.FeatureSelection;
        //    pFeature = pEnumFeature.Next();

        //    ESRI.ArcGIS.Geometry.IEnvelope pEnvelope;
        //    pEnvelope = new EnvelopeClass();

        //    while (pFeature != null)
        //    {
        //        pEnvelope.Union(pFeature.Shape.Envelope);
        //        pFeature = pEnumFeature.Next();
        //    }

        //    pEnvelope.Expand(1.2, 1.2, true);
        //    pActiveView = m_hookHelper.ActiveView;
        //    pActiveView.Extent = pEnvelope;
        //    pActiveView.Refresh();
        //}

        //////////////////////////////////
        // load theo dataset

        //string file;
        //   string[] nameLayer;
        //   IFeatureDataset pFeatureDataset;
        //   IEnumDataset pEnumDataset, pEnumDataset1;
        //   IDataset pDataset, pDataset1;
        //   IFeatureWorkspace pWS;

        //   pWS = (IFeatureWorkspace)generalVariable.ICurrentWorkspace;
        //   pEnumDataset = generalVariable.ICurrentWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);

        //   while ((pDataset = pEnumDataset.Next()) != null)
        //   {
        //       pFeatureDataset = pWS.OpenFeatureDataset(pDataset.Name);

        //       pEnumDataset1 = pFeatureDataset.Subsets;

        //       while ((pDataset1 = pEnumDataset1.Next()) != null)
        //       {
        //           if (pDataset1.Type  == esriDatasetType.esriDTFeatureClass)
        //           {
        //               file =  pDataset1.Name;
        //               nameLayer = file.Split('.');
        //               this.addLayer(file, generalVariable.ICurrentWorkspace, axMapControl, nameLayer[nameLayer.Length -1]);
        //           }
        //       }
        //   }

        /////////////////////////////////

    }
}
